<?php

namespace App\Http\Controllers;

use App\Cate;
use App\Video;
use Illuminate\Http\Request;
use vod\Request\V20170321 as vod;
use Illuminate\Support\Facades\Storage;

class VideoController extends Controller
{

    protected $accessKeyId = 'LTAIuOow4gm3Gh8H';
    protected $accessKeySecret = 'TxIj00uboJK6qLztbVGd1vP6YndcLU';
    protected $client;

    function __construct() {
        $regionId = 'cn-shanghai';  // 点播服务所在的Region，国内请填cn-shanghai，不要填写别的区域
        $profile = \DefaultProfile::getProfile($regionId, $this->accessKeyId, $this->accessKeySecret);
        $this->client =  new \DefaultAcsClient($profile);
    }

    // $client为上面init_vod_client接口返回的客户端对象
    // $videoId为视频ID
    function get_play_info($videoId) {
        try{
            $request = new vod\GetPlayInfoRequest();
            $request->setVideoId($videoId);
            $request->setAcceptFormat('JSON');
            dd($this->client->getAcsResponse($request));
        }catch (Exception $e){
            dd($e->getMessage());
        }

    }

    function create_upload_video(Request $request){
        $request_video = new vod\CreateUploadVideoRequest();
        //视频源文件标题(必选)
        $request_video->setTitle($request->input('video_title'));
        //视频源文件名称，必须包含扩展名(必选)
        $request_video->setFileName($request->input('video_name'));
        $response = $this->client->getAcsResponse($request_video);
        if($request->hasFile('FileUpload')) {
            //获取文件，file对应的是前端表单上传input的name
            $file =$request->file("FileUpload");
            //随机一个新的文件名
            $fileName  =md5($file->getClientOriginalName().time().rand()).".".$file->getClientOriginalExtension();
            $bool = Storage::put('uploads/'.$fileName,file_get_contents($file->getRealPath()));

            $data['picname'] = $fileName;


        }
        return response()->json([
            'uploadAuth' => $response->UploadAuth,
            'uploadAddress' => $response->UploadAddress,
            'videoId' => $response->VideoId
        ]);
    }

    function save_video_info(Request $request){
        $video_id = $request->input('video_id');
        $cate_id = $request->input('cate_id');
        $video_title = $request->input('video_title');
        $description = $request->input('description');
        $price = $request->input('price');
        $video = new Video;
        if($request->hasFile('picture')) {
            $file =$request->file("picture");
            $fileName  =md5($file->getClientOriginalName().time().rand()).".".$file->getClientOriginalExtension();
            $bool = Storage::put('uploads/picture/'.date('Ymd').'/'.$fileName,file_get_contents($file->getRealPath()));
            $video->picture = 'uploads/picture/'.date('Ymd').'/'.$fileName;
        }
        $video->price = $price;
        $video->title = $video_title;
        $video->description = $description;
        $video->path = $video_id;
        $video->cate = $cate_id;
        $video->save();
        return response()->json([
            'code'=>200,
            'status'=>'ok'
        ]);
    }

    function upload(){
        $cate = new Cate();
        $cate_list = $cate::all();
        return view('video/upload',['cate_list'=>$cate_list]);
    }

    function upload_img(Request $request){
        if(request()->isMethod('post')){
            if($request->hasFile('picture')) {
                $file =$request->file("picture");
                $fileName  =md5($file->getClientOriginalName().time().rand()).".".$file->getClientOriginalExtension();
                $bool = Storage::put('uploads/picture/'.$fileName,file_get_contents($file->getRealPath()));
                dd($fileName);
            }
        }else{
            return view('video/upload_img');
        }
        return 1;
    }

}
